#include "bookinfo.h"
#include "ui_bookinfo.h"
#include "utils.h"
#include "bookstore.h"
#include <QMessageBox>
using namespace std;
using namespace student;
using namespace bookandrecord;
using namespace bookstore;
using namespace utils;

void bookinfo::clearbook_line1() {
    ui->label_71->clear();
    ui->label_72->clear();
    ui->label_73->clear();
    ui->label_74->clear();
//    ui->comboBox->setEnabled(false);
//    ui->comboBox->hide();
}
void bookinfo::clearbook_line2() {
    ui->label_81->clear();
    ui->label_82->clear();
    ui->label_83->clear();
    ui->label_84->clear();
//    ui->comboBox_2->setEnabled(false);
//    ui->comboBox_2->hide();
}
void bookinfo::clearbook_line3() {
    ui->label_91->clear();
    ui->label_92->clear();
    ui->label_93->clear();
    ui->label_94->clear();
//    ui->comboBox_3->setEnabled(false);
//    ui->comboBox_3->hide();
}

void bookinfo::F5(){
    confbook_num=1;
    confnum=1;
    page_book=1;
    confbook_num=1;
    showbookinfo(confnum);
}
bookinfo::bookinfo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::bookinfo) {

    ui->setupUi(this);
    page_book = 1;
    pagesize = 3;
    pagebook_num = 1;
    confbook_num = 1;
    confnum = 1;

}

bookinfo::~bookinfo() {
    delete ui;
    delete thismenu;
}
void bookinfo::getData(Menu *menu) {
    thismenu = menu;
    QString menu_name = str2Qstr(menu->name);
    ui->lineEdit->setText(menu_name);
    QString menu_isbn = str2Qstr(menu->ISBN);
    ui->lineEdit_3->setText(menu_isbn);
    QString menu_writer = str2Qstr(menu->writer);
    ui->lineEdit_2->setText(menu_writer);
    QString menu_pub = str2Qstr(menu->publish);
    ui->lineEdit_4->setText(menu_pub);
    QString menu_booknum = QString(QString::number(menu->countNum));
    ui->label_6->setText(menu_booknum);
    F5();

}

void bookinfo::showbookinfo(int confnum = 1) {
    vector<Book*>().swap(bookList);

    Menu *menu=new Menu();
    menu= search<Menu>(thismenu->ID, menuDatPath);
    bookList = search_multiplebookReturn("notFuzzySearch", "menu_id", thismenu->ID, bookDatPath);
    pagebook_num=(bookList.size()+pagesize-1)/pagesize;
    int listnum=bookList.size();

        int i = (confnum-1)*pagesize;

        QString tempCell ;
        tempCell= QString(QString::number(bookList[i]->ID));
        ui->label_71->setText(tempCell);
        const char* s1="未借出";
        const char* s2="已借出";
        if((bookList[i]->isBorrowed)==-1){
            tempCell=str2Qstr(UtfToGbk(s1));
        }
        else
        {tempCell=str2Qstr(UtfToGbk(s2));}
        ui->label_72->setText(tempCell);
        const char* s3="未预约";
        const char* s4="已预约";
        if((bookList[i]->isBooked)==-1){

            tempCell=str2Qstr(UtfToGbk(s3));
        }
        else
        {tempCell=str2Qstr(UtfToGbk(s4));}
        ui->label_73->setText(tempCell);
        tempCell = QString(QString::number(bookList[i]->borrow_num));
        ui->label_74->setText(tempCell);
        ui->comboBox->setEnabled(true);
        ui->comboBox->setCurrentIndex(bookList[i]->book_cond);
        ui->comboBox->show();
        ui->pushButton_75->setEnabled(true);
        ui->pushButton_75->show();

        if(i+1>=bookList.size()){
            clearbook_line2();
            if(confnum==pagebook_num)
            {
                ui->comboBox_2->setEnabled(false);
                ui->comboBox_2->hide();
                ui->pushButton_85->setEnabled(false);
                ui->pushButton_85->hide();

            }
        }
        else
        {
            tempCell = QString(QString::number(bookList[i+1]->ID));
            ui->label_81->setText(tempCell);
            const char* s1="未借出";
            const char* s2="已借出";
            if((bookList[i+1]->isBorrowed)==-1){

                tempCell=str2Qstr(UtfToGbk(s1));
            }
            else
            {tempCell=str2Qstr(UtfToGbk(s2));}
            ui->label_82->setText(tempCell);
            const char* s3="未预约";
            const char* s4="已预约";
            if((bookList[i+1]->isBooked)==-1){

                tempCell=str2Qstr(UtfToGbk(s3));
            }
            else
            {tempCell=str2Qstr(UtfToGbk(s4));}
            ui->label_83->setText(tempCell);
            tempCell = QString(QString::number(bookList[i+1]->borrow_num));
            ui->label_84->setText(tempCell);
            ui->comboBox_2->setEnabled(true);
            ui->comboBox_2->setCurrentIndex(bookList[i+1]->book_cond);

            ui->comboBox_2->show();
            ui->pushButton_85->setEnabled(true);
            ui->pushButton_85->show();
        }

        if(i+2>=bookList.size()){
            clearbook_line3();
            if(confnum==pagebook_num)
            {
                ui->comboBox_3->setEnabled(false);
                ui->comboBox_3->hide();
                ui->pushButton_95->setEnabled(false);
                ui->pushButton_95->hide();
            }
        }
        else
        {
            tempCell = QString(QString::number(bookList[i+2]->ID));
            ui->label_91->setText(tempCell);
            const char* s1="未借出";
            const char* s2="已借出";
            if((bookList[i+2]->isBorrowed)==-1){
                tempCell=str2Qstr(UtfToGbk(s1));
            }
            else
            {tempCell=str2Qstr(UtfToGbk(s2));}
            ui->label_92->setText(tempCell);
            const char* s3="未预约";
            const char* s4="已预约";
            if((bookList[i+2]->isBooked)==-1){

                tempCell=str2Qstr(UtfToGbk(s3));
            }
            else
            {tempCell=str2Qstr(UtfToGbk(s4));}
            ui->label_93->setText(tempCell);
            tempCell = QString(QString::number(bookList[i+2]->borrow_num));
            ui->label_94->setText(tempCell);
            ui->comboBox_3->setEnabled(true);
            ui->comboBox_3->setCurrentIndex(bookList[i+2]->book_cond);

            ui->comboBox_3->show();
            ui->pushButton_95->setEnabled(true);
            ui->pushButton_95->show();
        }

        tempCell = QString(QString::number(pagebook_num));
        ui->label_24->setText(tempCell);
        tempCell = QString(QString::number(listnum));
        ui->label_6->setText(tempCell);
        tempCell = QString(QString::number(confnum));
        ui->lineEdit_5->setText(tempCell);


}

void bookinfo::on_pushButton_6_clicked()
{
    F5();
}
//上一页
void bookinfo::on_pushButton_3_clicked() {
    confnum = page_book - 1;
    page_book--;
    if(page_book < 1)
    {QMessageBox::warning(this, "warning", "已经是第一页");F5();return;}
    else
        this->showbookinfo(confnum);
}
//下一页
void bookinfo::on_pushButton_4_clicked() {
    confnum = page_book + 1;
    page_book++;
    if(page_book > pagebook_num)
    {QMessageBox::warning(this, "warning", "已经是最后一页");
        page_book=pagebook_num;
        return;}
    else
        this->showbookinfo(confnum);
}
//跳转
void bookinfo::on_pushButton_5_clicked() {
    QString str_ = ui->lineEdit_5->text();
    if(isDigitStr(str_))
    {

        if(confnum>pagebook_num)
        {
            F5();
            QMessageBox::warning(this,"warning","超过页码");
            ui->lineEdit_5->setText(QString :: number(confnum));
        }
        else
        {
            confnum = str_.toInt();
            page_book = confnum;
            this->showbookinfo(confnum);
        }
    }
    else
    { QMessageBox::warning(this,"warning","输入正确页码");F5();ui->lineEdit_5->setText(QString :: number(confnum));}
}

//添加书籍（写一个页面传递添加的本数）
void bookinfo::on_pushButton_clicked()
{
    QString str_ = ui->lineEdit_6->text();
    if(isDigitStr(str_)){
        int addbooknum =str_.toInt();
        if(addbooknum>0)
            addMenu(thismenu,addbooknum);
        else if(addbooknum>1000)
        {
            QMessageBox::warning(this,"warning","不得添加超过1000本书");
            return;
        }
        else
        {QMessageBox::warning(this,"warning","输入大于0的数字");return ;}
    }
    else
    { QMessageBox::warning(this,"warning","输入错误");return;}
    QMessageBox::information(this,"info","添加成功");
}
//删除book
void bookinfo::on_pushButton_75_clicked() {
    Book* book = bookList[(page_book - 1) * pagesize];
    if(deleteBook(book->ID) == 0) {
        QMessageBox::warning(this, "del", "确认删除");
        F5();
    }
    else if(deleteBook(book->ID) == 1)
    {
        QMessageBox::warning(this, "del", "确认删除,且书籍目录也会删除");
        F5();
    }
    else
        QMessageBox::warning(this, "del", "无法删除");
}

void bookinfo::on_pushButton_85_clicked() {
    Book* book = bookList[(page_book - 1) * pagesize + 1];
    if(deleteBook(book->ID) == 0) {
        QMessageBox::warning(this, "del", "确认删除");
        F5();
    }
    else if(deleteBook(book->ID) == 1)
    {
        QMessageBox::warning(this, "del", "确认删除,且书籍目录也会删除");
        F5();
    }
    else
        QMessageBox::warning(this, "del", "无法删除");
}

void bookinfo::on_pushButton_95_clicked() {
    Book* book = bookList[(page_book - 1) * pagesize + 2];
    if(deleteBook(book->ID) == 1) {
        QMessageBox::warning(this, "del", "确认删除");
        this->showbookinfo(confnum);
    }
    else if(deleteBook(book->ID) == 1)
    {
        QMessageBox::warning(this, "del", "确认删除,且书籍目录也会删除");
        F5();
    }
    else
        QMessageBox::warning(this, "del", "无法删除");
}
//修改信息
void bookinfo::on_pushButton_2_clicked() {
    QString str = ui->lineEdit->text();
    string bookname = string((const char *)str.toLocal8Bit());
    str = ui->lineEdit->text();
    if(bookname.size()==0)
    {
        QMessageBox::warning(this,"warning","请输入书籍名称");
        ui->lineEdit->setText(thismenu->name);
        return;
    }
    else if(judgeSpace(bookname)==0){
        QMessageBox::warning(this,"warning","请输入正确书籍名称");
        ui->lineEdit->setText(thismenu->name);
        return;
    }
    else if(bookname.length()>96)
    {
        QMessageBox::warning(this,"warning","书籍名称不能大于96字节");
        ui->lineEdit->setText(thismenu->name);
        return;
    }
    else
        ;

    str = ui->lineEdit_3->text();    string bookISBN = string((const char *)str.toLocal8Bit());
    if(bookISBN.size()==0)
    {
        QMessageBox::warning(this,"warning","请输入ISBN");
        ui->lineEdit_3->setText(thismenu->ISBN);
        return;
    }
    else if(judge_menu(bookISBN)==-1)
    {
        QMessageBox::warning(this,"warning","ISBN格式有误");
        ui->lineEdit_3->setText(thismenu->ISBN);
        return;
    }
    else
        ;

    str = ui->lineEdit_2->text();    string bookwriter = string((const char *)str.toLocal8Bit());
    if(bookwriter.size()==0)
    {
        QMessageBox::warning(this,"warning","请输入作者名称");
        ui->lineEdit_2->setText(thismenu->writer);
        return;
    }
    else if(bookwriter.length()>96)
    {
        QMessageBox::warning(this,"warning","作者名称不能大于96字节");
        ui->lineEdit_2->setText(thismenu->writer);
        return;
    }
    else if(judgeSpace(bookwriter)==0){
        QMessageBox::warning(this,"warning","请输入正确作者名称，若无则填写无");
        ui->lineEdit_2->setText(thismenu->writer);
        return;
    }
    else
        ;
        str = ui->lineEdit_4->text();
    string bookpublish = string((const char *)str.toLocal8Bit());
    if(bookpublish.size()==0)
    {
        QMessageBox::warning(this,"warning","请输入出版社名称");
        ui->lineEdit_4->setText(thismenu->publish);
        return;
    }
    else if(judgeSpace(bookpublish)==0){
        QMessageBox::warning(this,"warning","请输入正确出版社名称");
        ui->lineEdit_4->setText(thismenu->publish);
        return;
    }
    else if(bookpublish.length()>96)
    {
        QMessageBox::warning(this,"warning","出版社名称不能大于96字节");
        ui->lineEdit_4->setText(thismenu->publish);
        return;
    }

    else
        ;
    if(  strcmp(thismenu->name, &bookname[0])==0&&
         strcmp(thismenu->ISBN, &bookISBN[0])==0&&
         strcmp(thismenu->publish, &bookpublish[0])==0&&
         strcmp(thismenu->writer, &bookwriter[0])==0)
    {
        QMessageBox::warning(this,"warning","未修改");
    }
    else
    {
        strcpy(thismenu->name, &bookname[0]);
        strcpy(thismenu->ISBN, &bookISBN[0]);
        strcpy(thismenu->publish, &bookpublish[0]);
        strcpy(thismenu->writer, &bookwriter[0]);

        rewrite(thismenu->ID, thismenu, menuDatPath);
        QMessageBox::information(this,"info","修改成功");
    }

}

void bookinfo::on_comboBox_currentIndexChanged(int index)
{
    Book* book=bookList[(page_book-1)*pagesize];
    book->book_cond=index;
    rewrite<Book>(book->ID, book, bookDatPath);
}

void bookinfo::on_comboBox_2_currentIndexChanged(int index)
{
    Book* book=bookList[(page_book-1)*pagesize+1];
    book->book_cond=index;
    rewrite<Book>(book->ID, book, bookDatPath);
}

void bookinfo::on_comboBox_3_currentIndexChanged(int index)
{
    Book* book=bookList[(page_book-1)*pagesize+2];
    book->book_cond=index;
    rewrite<Book>(book->ID, book, bookDatPath);
}
